<?php
include '../../auth/ceklogin.php';
include '../../sql/conn.php';
if(!isset($_REQUEST['temp'])){ //jika menekan finish saat membuat quotation baru
    if(isset($_SESSION['usedinquiry'])){
        $noInquiry=$_SESSION['usedinquiry'];
        $username=$_SESSION['userbkknt'];
        
        //ambil lokasi user
        $query_ambillokasiuser=mysql_query("select lokasiUser from user where uname='$username'");
        $row_ambillokasiuser=mysql_fetch_array($query_ambillokasiuser);
        $lokasiUser=$row_ambillokasiuser[0];
        if($lokasiUser=='surabaya'){
            $lokasiUser="SBY";
        }elseif($lokasiUser=='jakarta'){
            $lokasiUser="JKT";
        }
        
        //ambil nomor quotation terakhir
        $query_finish_1=mysql_query("
            select noQuotation 
            from quotation 
            where noQuotation not like 'temp%' 
            order by idQuotation desc 
            limit 0,1
            ");
        $row_finish_1=mysql_fetch_array($query_finish_1);
        $tmp=explode("/",$row_finish_1[0]);

        //tahun & bulan
        $tahun_tmp=$tmp[4];
        $bulan_tmp=$tmp[3];

        //cek bulan & tahun sekarang apakah sudah ganti bulan ato tahun
        if($tahun_tmp==date("Y")){
                if($bulan_tmp==date("m")){
                        $new=$tmp[0]+1;
                }
                else{
                        $new=1;
                }
        }
        else{
                $new=1;
        }

        //nomor baru
        $len=strlen($new);
        for($len;$len<4;$len++){
        $new="0".$new;
        }

        $no = $new." /SV/BIA-".$lokasiUser."/".date("m")."/".date("Y");

        $query_finish_2=mysql_query("UPDATE inquiry SET chgby='$username',chgdate=now(), status='QUCR' WHERE no_inquiry='$noInquiry'");
        $query_finish_3=mysql_query("UPDATE quotation SET noQuotation='$no', statusQuotation='created' WHERE no_inquiry='$noInquiry'");
        
        if($query_finish_2 && $query_finish_3){
            $_SESSION['usedinquiry']='';
            echo json_encode(array('success'=>true));
        } else{
            echo json_encode(array('msg'=>'Query Error.'.  mysql_error()));
        }
    } else {
        echo json_encode(array('msg'=>'Error session not set'));
    }
}
else { 
    if($_REQUEST['temp']=='x'){ //jika menekan tombol tambah barang saat new quotation
        if(isset($_SESSION['usedinquiry'])){
            $noInquiry=$_SESSION['usedinquiry'];
            //ngambil no id quotation temporary
            $query_additem_temp_1=mysql_query("select idQuotation from quotation where no_inquiry='$noInquiry'");
            $idQuotation_temp=mysql_fetch_array($query_additem_temp_1);
            $idQuotation_temp=$idQuotation_temp[0];
            //ngambil urutan idQuotation_item terakhir
            $query_additem_temp_2=mysql_query("select idQuotation_item from quotation_item order by idQuotation_item desc limit 1");
            $idQuotation_item=mysql_fetch_array($query_additem_temp_2);
            $idQuotation_item=$idQuotation_item[0]+1;

            //ngambil data parsingan
            $idBarang=$_REQUEST['idBarang'];
            $qtyBarang=$_REQUEST['qtyBarang'];
            $priceItem=$_REQUEST['priceItem'];

            $query_additem_temp_3=mysql_query("INSERT INTO quotation_item VALUES ('$idQuotation_item','$idQuotation_temp','$idBarang','$qtyBarang','$priceItem')");
            if($query_additem_temp_3){
                echo json_encode(array('success'=>true));
            } else {
                echo json_encode(array('msg'=>mysql_error()));
            }
        }
        else{
            echo json_encode(array('msg'=>'Inquiry session not set.'));
        }
    }
    elseif($_REQUEST['temp']=='editx'){
        $idQuotation=$_REQUEST['idQuotation'];

        //ngambil urutan idQuotation_item terakhir
        $query_additem_temp_2=mysql_query("select idQuotation_item from quotation_item order by idQuotation_item desc limit 1");
        $idQuotation_item=mysql_fetch_array($query_additem_temp_2);
        $idQuotation_item=$idQuotation_item[0]+1;
        
        //ngambil data parsingan
        $idBarang=$_REQUEST['idBarang'];
        $qtyBarang=$_REQUEST['qtyBarang'];
        $priceItem=$_REQUEST['priceItem'];

        $query_edit_additem_temp_1=mysql_query("INSERT INTO quotation_item VALUES ('$idQuotation_item','$idQuotation','$idBarang','$qtyBarang','temp$priceItem')");
        if($query_edit_additem_temp_1){
            echo json_encode(array('success'=>true));
        } else {
            echo json_encode(array('msg'=>mysql_error()));
        }
        
    }
}
?>